{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1oGSPrFJM3T6kXazlP77kMFnXHTeAmIzl?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### Quick Start: Using Google’s Gemini LLM API with LangChain\n",
        "Easily connect Gemini with LangChain AI framework to build and run AI based applications.\n",
        "\n",
        "1. [Get Started with Gemini API](https://ai.google.dev/gemini-api/docs)\n",
        "2. [Learn LangChain Basics](https://python.langchain.com/docs/tutorials/)\n"
      ],
      "metadata": {
        "id": "HxxOWz9dpsYj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Install required libraries\n",
        "\n",
        "If you encounter an error during `pip install`, simply rerun the cell."
      ],
      "metadata": {
        "id": "Y3axTI0sp5Hg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -qU langchain-google-genai>=2.1.0"
      ],
      "metadata": {
        "id": "ShxTNxM5gqtr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Import related libraries"
      ],
      "metadata": {
        "id": "9jJ1vqs-p_Zx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_google_genai import ChatGoogleGenerativeAI\n",
        "import getpass\n",
        "import os"
      ],
      "metadata": {
        "id": "RL-3LsYogoH5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Provide a Google's Gemini API key (Free tier available)\n",
        "\n",
        "[Gemini API Key Generation Link](https://aistudio.google.com/apikey)\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "F6UeDlrgqI2A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"GOOGLE_API_KEY\"] = getpass.getpass()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yobvrD3glfd4",
        "outputId": "684f87f8-10eb-4382-c440-c2884635557d"
      },
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Run the Program\n",
        "1. [LangChain Doc](https://python.langchain.com/docs/integrations/chat/google_generative_ai/)\n",
        "2. [Gemini Models](https://ai.google.dev/gemini-api/docs/models)\n",
        "\n",
        "Note: If the latest model is rate-limited on the free tier, use lighter versions like `gemini-2.0-flash` or `gemini-2.0-flash-lite`.\n",
        "\n",
        "### Understanding the Parameters\n",
        "\n",
        "1. `temperature` → Think of this as creativity level.\n",
        "    - 0 = very strict, almost always gives the same answer.\n",
        "    - 1 = more random, tries different ways of answering.\n",
        "\n",
        "\n",
        "2. `top_p` → Imagine the model has a bag of possible next words.\n",
        "    - 0.9 means: only keep the words that together cover 90% of the likelihood, then pick from them.\n",
        "    - Helps cut off very unlikely/weird words.\n",
        "\n",
        "\n",
        "3. `top_k` → Another way of limiting choices.\n",
        "    - 40 means: only look at the 40 most likely words before picking one.\n",
        "    - Smaller k = safer, larger k = more variety.\n",
        "\n",
        "\n",
        "4. `max_output_tokens` → Maximum length of the answer.\n",
        "    - 512 = the model will stop once it has generated about 512 tokens (roughly 350-400 words)."
      ],
      "metadata": {
        "id": "YS35lwtUZ2eL"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatGoogleGenerativeAI(\n",
        "    model=\"gemini-2.0-flash\", #gemini-2.5-pro, gemini-2.5-flash, gemini-2.5-flash-lite, gemini-2.0-flash-lite\n",
        "    temperature=0.5,\n",
        "    top_p=0.9,\n",
        "    top_k=40,\n",
        "    max_output_tokens=512\n",
        ")"
      ],
      "metadata": {
        "id": "HHXlnTcxpi-8"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### A. Basic LLM Call"
      ],
      "metadata": {
        "id": "TayFIVu4GVaJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "ai_msg = llm.invoke(\"Provide python code of sudoku\")\n",
        "print(ai_msg.content)"
      ],
      "metadata": {
        "id": "f4OZQ-kvqNl5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### B. Prompt Template Example with System and Human Roles"
      ],
      "metadata": {
        "id": "JUZ5M9TDGzrR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_core.prompts import ChatPromptTemplate\n",
        "\n",
        "prompt = ChatPromptTemplate.from_messages([\n",
        "    (\"system\", \"You are a helpful assistant that explains problems step-by-step.\"),\n",
        "    (\"human\", \"Solve this problem step by step: {problem}\"),\n",
        "])\n",
        "\n",
        "# Chaining Prompt Template with LLM\n",
        "chain = prompt | llm\n",
        "\n",
        "ai_msg = chain.invoke({\"problem\": \"Provide python code of sudoku\"})\n",
        "print(ai_msg.content)"
      ],
      "metadata": {
        "id": "O9FPOsu4G09k"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}